$(function() {
	$(".dept_select").attr("selected","selected");
	
	$("#search_dept_name_select").chosen({
		no_results_text: "没有找到匹配的记录"
	});
	
	var emp_id,emp_name,dept_name,page_num,offset,page_size;
	
	$.ajax({
		type:"POST",
		url:"position/getSelectList",
		success: function(response) {
			var jsonData = eval(response);
			//console.log(jsonData);
			for(var i=0;i<jsonData.depList.length;i++){
				var dep = jsonData.depList[i];
				$("#search_dept_name_select").append("<option value='"+dep.depName+"'>"+dep.depName+"</option>");
			}
			$("#search_dept_name_select").trigger("chosen:updated");
			
		}
	});
	
	//用户点击搜索按钮
	$(".emp_search_btn").click(function(){
		close_alert();
		
		emp_id = $("#emp_id-in-searchForm").val();
		emp_name = $("#emp_name-in-searchForm").val();
		dept_name = $("#search_dept_name_select").val();
		page_num = $(".page_num").val();
		offset = 0;
		
		ajax_emp_search(emp_id,emp_name,dept_name,page_num,true);
	});
	
	//发送异步搜索请求
	var ajax_emp_search = function(emp_id,emp_name,dept_name,page_num,refresh){
		$.ajax({
			// 发送方法
			type:"POST",
			// 发送url
			url:"emp/searchEmp",
			data:{
			"emp_id_string":emp_id,
			"emp_name":emp_name,
			"dep_name":dept_name,
			"offset":offset
			},
			// 成果返回后
			success: function(response) {
				var jsonData = eval(response);
				page_size = jsonData.page_size;
				if(jsonData.total_num == "0"){
					alert_div($(".alert_container"),"没有员工信息");
					$(".emp_search_container").html("");
				}
				else{
					if(refresh){
						close_alert();
					}
					ajax_load_emp_search(response);
				}
			}
		});
	}
	
	//加载日报搜索列表
	var ajax_load_emp_search = function(emp_list_data){
		$.ajax({
			type:"GET",
			url:"vmresult/position/emp-search.html",
			success: function(html) {
				//console
				console.log(emp_list_data);
				$(".emp_search_container").html("");
				$(".emp_search_container").html(html);
				
				var jsonData = eval(emp_list_data);
				//获取列表显示
				var listHtml = getSearchList(jsonData.response);
				$(".emp_search_body").replaceWith(listHtml);
				//设置table foot
				$(".page_num").text(jsonData.page_num);
				$(".total_page").text(jsonData.total_page);
				$(".total_num").text(jsonData.total_num);
				//点击首页按钮
				$(".first_page_btn").click(function(){
					close_alert();
					
					var page_num = jsonData.page_num;
					if(page_num == 1)
						alert("已经是首页");
					else
						offset = 0;
						ajax_emp_search(emp_id,emp_name,dept_name,1);
				});
				//点击前一页按钮
				$(".pre_page_btn").click(function(){
					close_alert();
					
					var page_num = jsonData.page_num;
					if(page_num == 1)
						alert("已经是首页");
					else
						offset = offset-page_size;
						ajax_emp_search(emp_id,emp_name,dept_name,page_num-1);
				});
				//点击下一页按钮
				$(".next_page_btn").click(function(){
					close_alert();
					
					var page_num = jsonData.page_num;
					var total_page = jsonData.total_page;
					if(page_num == total_page)
						alert("没有更多页面");
					else
						offset = offset+page_size;
						ajax_emp_search(emp_id,emp_name,dept_name,page_num+1);
				});
				//点击尾页按钮
				$(".last_page_btn").click(function(){
					close_alert();
					
					var page_num = jsonData.page_num;
					var total_page = jsonData.total_page;
					if(page_num == total_page)
						alert("没有更多页面");
					else
						offset = (total_page-1)*page_size;
						ajax_emp_search(emp_id,emp_name,dept_name,jsonData.total_page);
				});
				//点击添加
				$(".emp_create_btn").click(function(){
					$("#add_modal").modal('toggle');
					
					$("#add_modal").on('shown.bs.modal', function () {
						   $('.chosen-select').chosen({
							   no_results_text: "没有找到匹配的记录"
						   }); 
						 })
					
					$.ajax({
						type:"POST",
						url:"position/getSelectList",
						success: function(response) {
							var jsonData = eval(response);
							$(".add_emp_dept").html("");
							$(".add_emp_dept").append("<option value='' class='none_dept_select-in-add' selected></option>");
							for(var i=0;i<jsonData.depList.length;i++){
								var dep = jsonData.depList[i];
								$(".add_emp_dept").append("<option value='"+dep.depId+"'>"+dep.depName+"</option>");
							}
							$(".chosen-select").trigger("chosen:updated");
							$(".add_emp_dept").change(function(){
								var dep_id_select = $(".add_emp_dept").val();
								$.ajax({
									type:"POST",
									data:{
										"dep_id_string":dep_id_select
									},
									url:"emp/getPosSelectList",
									success:function(data){
										var jsonData = eval(data);
										$(".add_emp_position").html("");
										$(".add_emp_position").append("<option value='' class='none_emp_select-in-add' selected></option>");
										for(var i=0;i<jsonData.posList.length;i++){
											var pos = jsonData.posList[i];
											$(".add_emp_position").append("<option value='"+pos.positionId+"'>"+pos.positionName+"</option>");
										}
										$(".chosen-select").trigger("chosen:updated");
									}
								});
							});
							
						}
					});
					ajax_add_emp();
				});
				//点击查看员工
				$(".view_emp_btn").on("click",function(){
					close_alert();
					
					var empId = $(this).attr("emp_id");
					
					$("#view_emp_modal").modal('toggle');
					
					$.ajax({
						type:"POST",
						url:"position/getSelectList",
						success: function(response) {
							var jsonData = eval(response);
							$(".view_emp_dept").append("<option value='' class='none_dept_select-in-view' selected></option>");
							for(var i=0;i<jsonData.depList.length;i++){
								var dep = jsonData.depList[i];
								$(".view_emp_dept").append("<option value='"+dep.depId+"'>"+dep.depName+"</option>");
							}
							$.ajax({
								type:"POST",
								url:"emp/getPositionList",
								success: function(response) {
									var jsonData = eval(response);
									$(".view_emp_position").append("<option value='' class='none_position_select-in-view' selected></option>");
									for(var i=0;i<jsonData.posList.length;i++){
										var pos = jsonData.posList[i];
										$(".view_emp_position").append("<option value='"+pos.positionId+"'>"+pos.positionName+"</option>");
									}
									ajax_view_emp(empId);
								}
							});
							
						}
					});
				});
				//点击修改
				$(".emp_update_btn").on("click",function(){
					close_alert();
					
					var empId = $(this).attr("emp_id");					
					$("#edit_modal").modal('toggle'); 
					
					$("#edit_modal").on('shown.bs.modal', function () {
						   $('.chzn-select').chosen({
							   no_results_text: "没有找到匹配的记录"
						   }); 
						 })
					
					$.ajax({
						type:"POST",
						url:"position/getSelectList",
						success: function(response) {
							var jsonData = eval(response);
							$(".update_emp_dept").html("");
							$(".update_emp_dept").append("<option value='' class='none_dept_select-in-update' selected></option>");
							for(var i=0;i<jsonData.depList.length;i++){
								var dep = jsonData.depList[i];
								$(".update_emp_dept").append("<option value='"+dep.depId+"'>"+dep.depName+"</option>");
							}
							$(".chzn-select").trigger("chosen:updated");
							$.ajax({
								type:"POST",
								url:"emp/getPositionList",
								success: function(response) {
									var jsonData = eval(response);
									$(".update_emp_position").html("");
									$(".update_emp_position").append("<option value='' class='none_position_select-in-edit' selected></option>");
									for(var i=0;i<jsonData.posList.length;i++){
										var pos = jsonData.posList[i];
										$(".update_emp_position").append("<option value='"+pos.positionId+"'>"+pos.positionName+"</option>");
									}
									$(".chzn-select").trigger("chosen:updated");
									ajax_update_emp(empId);
								}
							});
							
							
						}
					});
					
//					ajax_update_emp(empId);
				});
				//点击删除
				$(".emp_delete_btn").on("click",function(){
					close_alert();
					
					var empId = $(this).attr("emp_id");
					$("#delete_modal").modal('toggle');
					
					$(".confirm_in_delete").click(function(){
						$("#delete_modal").modal('hide');
						$("#delete_modal").on('hidden.bs.modal',function (e){
							$.ajax({
								type:"POST",
								url:"emp/deleteEmp",
								data:{
									"emp_id_string":empId
								},
								// 成果返回后
								success: function(response) {
									var jsonData = eval(response);
									if(jsonData.messageType=="200"){
										alert_div($(".alert_container"),jsonData.message);
										ajax_emp_search(emp_id,emp_name,dept_name,page_num,false);
									}
									else 
										alert_div($(".alert_container"),jsonData.message);
								}
							});
						});
					});
					
				});
				
			}
		});

	}
	
	var getSearchList = function(list){
		var html = "<tbody class='emp_search_body'> ";
						for(var i=0;i<list.length;i++){
							var temp_data = list[i];
							var sex = null;
							if(temp_data.empSex==1){
								sex = "男";
							}
							else{
								sex = "女";
							}
							html+="<tr data-ng-repeat='row in tableRow'>" +
										
										"<td>"+temp_data.empId+"</td>" +
										"<td>"+temp_data.empName+"</td>" +
										"<td>"+sex+"</td>" +
										"<td>"+temp_data.depName+"</td>" +
										"<td>"+temp_data.positionName+"</td>" +
										"<td>" +
											"<div class='hidden-sm hidden-xs btn-group'>" +
													"<button class='btn btn-xs btn-success view_emp_btn' emp_id='"+temp_data.empId+"'>" +
														"<i class='ace-icon fa fa-search-plus bigger-120'></i>" +
													"</button>" +
													"<button class='btn btn-xs btn-info emp_update_btn' emp_id='"+temp_data.empId+"'>" +
														"<i class='ace-icon fa fa-pencil bigger-120'></i>" +
													"</button>" +
													"<button class='btn btn-xs btn-danger emp_delete_btn' emp_id='"+temp_data.empId+"'>" +
														"<i class='ace-icon fa fa-trash-o bigger-120'></i>" +
													"</button>" +
											"</div>" +
										"</td>" +
									"</tr>";
						}
		html+="</tbody>";
		return html;
	}
	
	var ajax_add_emp = function(){
		//获取填入的数据
		console.log("add");
		$("#add_emp_form").validate({
			rules : {
				add_emp_id:"required",
				add_emp_name:"required",
				add_sex:"required",
				add_birthDate:"required",
				add_idcard:"required",
				add_emp_dept:"required",
				add_emp_position:"required",
				add_empDate:"required",
				add_workDate:"required",
				add_emp_form:"required",
				add_emp_src:"required",
				add_graduateDate:"required"
			},
			messages : {
				add_emp_id:"员工编号不为空",
				add_emp_name:"员工名称不为空",
				add_sex:"性别不为空",
				add_birthDate:"出生日期不为空",
				add_idcard:"身份证号不为空",
				add_emp_dept:"部门不为空",
				add_emp_position:"岗位不为空",
				add_empDate:"入职日期不为空",
				add_workDate:"参加工作日期不为空",
				add_emp_form:"用工方式不为空",
				add_emp_src:"人员来源不为空",
				add_graduateDate:"毕业日期不为空"
			},
			errorPlacement: function (error,element) {
				if(element.parent().children('.error-message-mod').length == 0){
					error.appendTo(element.parent());
				} else {
					error.appendTo(element.parent().children('.error-message-mod'));
				}
			},
			submitHandler:function(form){
					var empName = $(".add_name_input").val();
					var empSex = $(".add_sex").val();
					var empBirthday = $(".add_birthDate").val();
					var empIdcard = $(".add_idcard_input").val();
					var empDept = $(".add_emp_dept").val();
					var empPosition = $(".add_emp_position").val();
					var empEmpDate = $(".add_empDate").val();
					var empWorkDate = $(".add_workDate").val();
					var empForm = $(".add_emp_form").val();
					var empSrc = $(".add_emp_src").val();
					var empPolitical = $(".add_emp_political").val();
					var empNation = $(".add_emp_nation").val();
					var empPhone = $(".add_emp_phone_input").val();
					var empEmail = $(".add_email_input").val();
					var empHeight = $(".add_emp_height_input").val();
					var empBlood = $(".add_emp_blood").val();
					var empMarry = $(".add_emp_marry").val();
					var empOrigin = $(".add_origin_input").val();
					var empBirthplace = $(".add_birth_place_input").val();
					var empPlace = $(".add_emp_place_input").val();
					var empEducation = $(".add_emp_education").val();
					var empDegree = $(".add_emp_degree").val();
					var empSchool = $(".add_emp_school_input").val();
					var empMajor = $(".add_emp_major_input").val();
					var empGraduateDate = $(".add_graduateDate").val();
					
					$("#add_modal").modal('hide');
					$("#add_modal").on('hidden.bs.modal',function (e){
						$.ajax({
							type:"POST",
							url:"emp/addEmp",
							data:{
								"empName":empName,
								"empSex":empSex,
								"empBirth":empBirthday,
								"empIdentity":empIdcard,
								"empJoinDate":empEmpDate,
								"empWorkDate":empWorkDate,
								"empType":empForm,
								"empSource":empSrc,
								"empPolStatus":empPolitical,
								"empMinzu":empNation,
								"empJiguan":empOrigin,
								"empPhone":empPhone,
								"empEmail":empEmail,
								"empHeight":empHeight,
								"empBlood":empBlood,
								"empMarriageType":empMarry,
								"empBirthPlace":empBirthplace,
								"empHukouPlace":empPlace,
								"empXueli":empEducation,
								"empXuewei":empDegree,
								"empGraduateSchool":empSchool,
								"empMajor":empMajor,
								"empGraduateDate":empGraduateDate,
								"depId":empDept,
								"positionId":empPosition
							},
							success: function(data) {
								var jsonData = eval(data);
								if(jsonData.messageType == "200"){
									alert_div($(".alert_container"),jsonData.message);
									ajax_emp_search(emp_id,emp_name,dept_name,page_num,false);
								}
								else
									alert_div($(".alert_container"),jsonData.message);
							}
						});
					});
			}   
		});
			
	}

	var ajax_update_emp = function(empId){
		$.ajax({
			type:"POST",
			data:{
				"emp_id_string":empId
			},
			url:"emp/viewEmp",
			success: function(page_data) {
				//添加显示数据
				var jsonData = eval(page_data);
				var emp = jsonData.response;
				console.log(emp);
				$(".update_id_input").val(emp.empId);
				$(".update_name_input").val(emp.empName);
				$(".update_sex").val(emp.empSex);
				$(".update_birthDate").val(emp.empBirth);
				$(".update_idcard_input").val(emp.empIdentity);
				$(".update_emp_dept").val(emp.depId);
				$(".update_emp_position").val(emp.positionId);
				$(".update_empDate").val(emp.empJoinDate);
				$(".update_workDate").val(emp.empWorkDate);
				$(".update_emp_form").val(emp.empType);
				$(".update_emp_src").val(emp.empSource);
				$(".update_emp_political").val(emp.empPolStatus);
				$(".update_emp_nation").val(emp.empMinzu);
				$(".update_emp_phone_input").val(emp.empPhone);
				$(".update_email_input").val(emp.empEmail);
				$(".update_emp_height_input").val(emp.empHeight);
				$(".update_emp_blood").val(emp.empBlood);
				$(".update_emp_marry").val(emp.empMarriageType);
				$(".update_origin_input").val(emp.empJiguan);
				$(".update_birth_place_input").val(emp.empBirthPlace);
				$(".update_emp_place_input").val(emp.empHukouPlace);
				$(".update_emp_education").val(emp.empXueli);
				$(".update_emp_degree").val(emp.empXuewei);
				$(".update_emp_school_input").val(emp.empGraduateSchool);
				$(".update_emp_major_input").val(emp.empMajor);
				$(".update_graduateDate").val(emp.empGraduateDate);
				
				$(".update_emp_dept").change(function(){
					var dep_id_select = $(".update_emp_dept").val();
					
					$.ajax({
						type:"POST",
						data:{
							"dep_id_string":dep_id_select
						},
						url:"emp/getPosSelectList",
						success:function(data){
							var jsonData = eval(data);
							$(".update_emp_position").html("");
							$(".update_emp_position").append("<option value='' class='none_position_select-in-update' selected></option>");
							for(var i=0;i<jsonData.posList.length;i++){
								var pos = jsonData.posList[i];
								$(".update_emp_position").append("<option value='"+pos.positionId+"'>"+pos.positionName+"</option>");
							}
							
						}
					});
					
				});
				
				$("#update_emp_form").validate({
					rules : {
						update_emp_id:"required",
						update_emp_name:"required",
						update_sex:"required",
						update_birthDate:"required",
						update_idcard:"required",
						update_emp_dept:"required",
						update_emp_position:"required",
						update_empDate:"required",
						update_workDate:"required",
						update_emp_form:"required",
						update_emp_src:"required",
						update_graduateDate:"required"
					},
					messages : {
						update_emp_id:"员工编号不为空",
						update_emp_name:"员工名称不为空",
						update_sex:"性别不为空",
						update_birthDate:"出生日期不为空",
						update_idcard:"身份证号不为空",
						update_emp_dept:"部门不为空",
						update_emp_position:"岗位不为空",
						update_empDate:"入职日期不为空",
						update_workDate:"参加工作日期不为空",
						update_emp_form:"用工方式不为空",
						update_emp_src:"人员来源不为空",
						update_graduateDate:"毕业日期不为空"
					},
					errorPlacement: function (error,element) {
						if(element.parent().children('.error-message-mod').length == 0){
							error.appendTo(element.parent());
						} else {
							error.appendTo(element.parent().children('.error-message-mod'));
						}
					},
					submitHandler:function(form){
						//提交更新
						var empName = $(".update_name_input").val();
						var empSex = $(".update_sex").val();
						var empBirthday = $(".update_birthDate").val();
						var empIdcard = $(".update_idcard_input").val();
						var empDept = $(".update_emp_dept").val();
						var empPosition = $(".update_emp_position").val();
						var empEmpDate = $(".update_empDate").val();
						var empWorkDate = $(".update_workDate").val();
						var empForm = $(".update_emp_form").val();
						var empSrc = $(".update_emp_src").val();
						var empPolitical = $(".update_emp_political").val();
						var empNation = $(".update_emp_nation").val();
						var empPhone = $(".update_emp_phone_input").val();
						var empEmail = $(".update_email_input").val();
						var empHeight = $(".update_emp_height_input").val();
						var empBlood = $(".update_emp_blood").val();
						var empMarry = $(".update_emp_marry").val();
						var empOrigin = $(".update_origin_input").val();
						var empBirthplace = $(".update_birth_place_input").val();
						var empPlace = $(".update_emp_place_input").val();
						var empEducation = $(".update_emp_education").val();
						var empDegree = $(".update_emp_degree").val();
						var empSchool = $(".update_emp_school_input").val();
						var empMajor = $(".update_emp_major_input").val();
						var empGraduateDate = $(".update_graduateDate").val();
						
						$("#edit_modal").modal('hide');
						$("#edit_modal").on('hidden.bs.modal',function (e){
							$.ajax({
								type:"POST",
								url:"emp/updateEmp",
								data:{
									"empId":empId,
									"empName":empName,
									"empSex":empSex,
									"empBirth":empBirthday,
									"empIdentity":empIdcard,
									"empJoinDate":empEmpDate,
									"empWorkDate":empWorkDate,
									"empType":empForm,
									"empSource":empSrc,
									"empPolStatus":empPolitical,
									"empMinzu":empNation,
									"empJiguan":empOrigin,
									"empPhone":empPhone,
									"empEmail":empEmail,
									"empHeight":empHeight,
									"empBlood":empBlood,
									"empMarriageType":empMarry,
									"empBirthPlace":empBirthplace,
									"empHukouPlace":empPlace,
									"empXueli":empEducation,
									"empXuewei":empDegree,
									"empGraduateSchool":empSchool,
									"empMajor":empMajor,
									"empGraduateDate":empGraduateDate,
									"depId":empDept,
									"positionId":empPosition
								},
								success: function(data) {
									var jsonData = eval(data);
									console.log(empForm);
									if(jsonData.messageType == "200"){
										alert_div($(".alert_container"),jsonData.message);
										ajax_emp_search(emp_id,emp_name,dept_name,page_num,false);
									}
									else
										alert_div($(".alert_container"),jsonData.message);
								}
							});
						});	
					}   
				});
				
			}
		});
	}
	
	var ajax_view_emp = function(empId){
		$.ajax({
			// 发送方法
			type:"POST",
			// 发送url
			url:"emp/viewEmp",
			data:{
			"emp_id_string":empId
			},
			// 成果返回后
			success: function(response) {
				var jsonData = eval(response);
				console.log(jsonData);
				var emp = jsonData.response;
				console.log(emp);
				$(".view_id_input").val(emp.empId);
				$(".view_name_input").val(emp.empName);
				$(".view_sex").val(emp.empSex);
				$(".view_birthDate").val(emp.empBirth);
				$(".view_idcard_input").val(emp.empIdentity);
				$(".view_emp_dept").val(emp.depId);
				$(".view_emp_position").val(emp.positionId);
				$(".view_empDate").val(emp.empJoinDate);
				$(".view_workDate").val(emp.empWorkDate);
				$(".view_emp_form").val(emp.empType);
				$(".view_emp_src").val(emp.empSource);
				$(".view_emp_political").val(emp.empPolStatus);
				$(".view_emp_nation").val(emp.empMinzu);
				$(".view_emp_phone_input").val(emp.empPhone);
				$(".view_email_input").val(emp.empEmail);
				$(".view_emp_height_input").val(emp.empHeight);
				$(".view_emp_blood").val(emp.empBlood);
				$(".view_emp_marry").val(emp.empMarriageType);
				$(".view_origin_input").val(emp.empJiguan);
				$(".view_birth_place_input").val(emp.empBirthPlace);
				$(".view_emp_place_input").val(emp.empHukouPlace);
				$(".view_emp_education").val(emp.empXueli);
				$(".view_emp_degree").val(emp.empXuewei);
				$(".view_emp_school_input").val(emp.empGraduateSchool);
				$(".view_emp_major_input").val(emp.empMajor);
				$(".view_graduateDate").val(emp.empGraduateDate);
			}
		});
	}
	
	var alert_div = function(container,msg){
		container.css("display","none");
		container.html("");
		var div = "<div class='alert_div alert alert-success alert-dismissible fade in' role='alert'>" +
						"<button type='button' class='close' data-dismiss='alert' aria-label='Close'>" +
							"<span aria-hidden='true'>×</span>" +
						"</button>" +
						"<strong class='show_ms'></strong>" +
				"</div>";
		container.html(div);
		$(".show_ms").text(msg);
		container.css("display","");
	}
	
	var close_alert = function(){
		$(".alert_container").html("");
		$(".alert_container").css("display","none");
	}
});


